package edu.ccut.saturn.component.loader.data;

import java.util.ArrayList;
import java.util.List;

import org.dom4j.Element;
import org.dom4j.Text;

import edu.ccut.saturn.component.ISaturnDataSqlItemInfo;

public abstract class AbstractSqlItemInfoLoader implements ISqlItemInfoLoader {

	public static final String NODE_KEY_PREPEND = "prepend";

	public static final String NODE_KEY_PROPERTY = "property";

	public static final String NODE_KEY_ISEQUAL_COMPAREVALUE = "compareValue";

	public static final String NODE_KEY_ISEQUAL_COMPAREPROPERTY = "compareProperty";

	public abstract ISaturnDataSqlItemInfo loadSqlItemInfo(Object element)
			throws Exception;

	@SuppressWarnings("unchecked")
	protected static List<ISaturnDataSqlItemInfo> getSqlItemInfos(
			List subElements) throws Exception {

		List elements = SqlLoader.formatElements(subElements);
		List<ISaturnDataSqlItemInfo> itemInfos = new ArrayList<ISaturnDataSqlItemInfo>();

		for (Object element : elements) {

			ISqlItemInfoLoader sqlItemInfoLoader = null;

			if (element instanceof Text) {
				sqlItemInfoLoader = SqlItemLoaderFactory.getInstance()
						.createSqlItemInfoLoader("text");

			} else if (element instanceof Element) {
				sqlItemInfoLoader = SqlItemLoaderFactory.getInstance()
						.createSqlItemInfoLoader(((Element) element).getName());

			}

			if (sqlItemInfoLoader != null) {
				ISaturnDataSqlItemInfo sqlItemInfo = sqlItemInfoLoader
						.loadSqlItemInfo(element);

				if (sqlItemInfo != null) {
					itemInfos.add(sqlItemInfo);
				}
			}
		}
		return itemInfos;
	}

}
